
Mock.mock('/generateClass','get',function(){

    return Mock.mock({
        'classes|10-50':[
            {
                'id|+1':1,
                'name':'F@',
                'age|20-30':1,
                'gender':/[男女]/
            }
        ]
    })
})

Mock.mock("/findClasses",'post',function(options){
    if(typeof options.body == "string"){
        options.body = JSON.parse(options.body);
    }
    let startDate = options.body.startDate;
    let endDate = options.body.endDate;
    let classes = JSON.parse(localStorage.getItem("classes") || "[]");
    if(startDate){
        classes = classes.filter(item => item.createDate >= startDate);
    }
    if(endDate){
        classes = classes.filter(item => item.createDate <= endDate);
    }
    return {
        code:0,
        data:classes
    };
});

Mock.mock('/addClass','post',function(options){
    let {name,count,teacher,createDate} = options.body;
    let classes = JSON.parse(localStorage.getItem('classes') || '[]');
    let id = 1;
    if(classes.length > 0){
        id = classes[classes.length - 1].id + 1;
    }
    classes.push({
        id,name,count,teacher,createDate
    });
    localStorage.setItem('classes',JSON.stringify(classes));
    return {
        msg:"增加成功"
    }
});

Mock.mock('/delClass','post',function(options){
    let {id} = options.body;
    let classes = JSON.parse(localStorage.getItem("classes") || "[]");
    classes = classes.filter(item => item.id != id);
    localStorage.setItem('classes',JSON.stringify(classes));
    return {
        msg:"删除成功"
    }
});
Mock.mock('/findById','post',function(options){
    let {id} = options.body;
    let classes = JSON.parse(localStorage.getItem("classes") || "[]");
    
    let cls = classes.find(item => item.id == id);
    
    return {
        cls
    }
});
Mock.mock('/updateClass','post',function(options){
    let {id,name,count,teacher,createDate} = options.body;
    let classes = JSON.parse(localStorage.getItem("classes") || "[]");
    let cls = classes.find(item => item.id == id);
    cls.name = name;
    cls.count = count;
    cls.teacher = teacher;
    cls.createDate = createDate;
    localStorage.setItem("classes",JSON.stringify(classes));
    return {
        msg:"修改成功"
    }
});